#include <stdio.h>
#include <stdlib.h>

#include "sys_inner.h"
#include "sys_file.h"

#define SYS_FILE_PATH_SIZE 1024
#define SYS_FILE_LINE_SIZE (1024 * 256)

extern SysInner *gSysInner;

// ----- 外部方法 -----

bool SysFileIsExist(const char* path)
{
    char finalPath[SYS_FILE_PATH_SIZE] = {0};
    gSysInner->platStorage->GetObsolutePath(path, finalPath, SYS_FILE_PATH_SIZE);
    return gSysInner->platStorage->IsExist(finalPath);
}

bool SysFileIsFolder(const char* path)
{
    char finalPath[SYS_FILE_PATH_SIZE] = {0};
    gSysInner->platStorage->GetObsolutePath(path, finalPath, SYS_FILE_PATH_SIZE);
    return gSysInner->platStorage->IsFolder(finalPath);
}

int32_t SysFileDelete(const char* path)
{
    char finalPath[SYS_FILE_PATH_SIZE] = {0};
    gSysInner->platStorage->GetObsolutePath(path, finalPath, SYS_FILE_PATH_SIZE);
    return gSysInner->platStorage->Delete(finalPath);
}

int32_t SysFileCreate(const char* path)
{
    char finalPath[SYS_FILE_PATH_SIZE] = {0};
    gSysInner->platStorage->GetObsolutePath(path, finalPath, SYS_FILE_PATH_SIZE);
    return gSysInner->platStorage->Create(finalPath);
}

int32_t SysFileSizeOf(const char* file)
{
    char finalPath[SYS_FILE_PATH_SIZE] = {0};
    gSysInner->platStorage->GetObsolutePath(file, finalPath, SYS_FILE_PATH_SIZE);
    return gSysInner->platStorage->SizeOf(finalPath);
}

int32_t SysFileCopy(const char* distPath, const char* srcPath)
{
    char finalDistPath[SYS_FILE_PATH_SIZE] = {0};
    char finalSrcPath[SYS_FILE_PATH_SIZE] = {0};
    gSysInner->platStorage->GetObsolutePath(distPath, finalDistPath, SYS_FILE_PATH_SIZE);
    gSysInner->platStorage->GetObsolutePath(srcPath, finalSrcPath, SYS_FILE_PATH_SIZE);
    return gSysInner->platStorage->Copy(finalDistPath, finalSrcPath);
}

int32_t SysFileMove(const char* distPath, const char* srcPath)
{
    char finalDistPath[SYS_FILE_PATH_SIZE] = {0};
    char finalSrcPath[SYS_FILE_PATH_SIZE] = {0};
    gSysInner->platStorage->GetObsolutePath(distPath, finalDistPath, SYS_FILE_PATH_SIZE);
    gSysInner->platStorage->GetObsolutePath(srcPath, finalSrcPath, SYS_FILE_PATH_SIZE);
    return gSysInner->platStorage->Move(finalDistPath, finalSrcPath);
}

int32_t SysFileReadLine(const char* file, char *data, int32_t size, int32_t lineIndex)
{
    char finalPath[SYS_FILE_PATH_SIZE] = {0};
    gSysInner->platStorage->GetObsolutePath(file, finalPath, SYS_FILE_PATH_SIZE);
    return gSysInner->platStorage->ReadLine(finalPath, data, size, lineIndex);
}

int32_t SysFileReadAll(const char* file, char *data, int32_t size)
{
    char finalPath[SYS_FILE_PATH_SIZE] = {0};
    gSysInner->platStorage->GetObsolutePath(file, finalPath, SYS_FILE_PATH_SIZE);
    return gSysInner->platStorage->ReadAll(finalPath, data, size);
}

int32_t SysFileWriteAll(const char* file, char *data, int32_t size)
{
    char finalPath[SYS_FILE_PATH_SIZE] = {0};
    gSysInner->platStorage->GetObsolutePath(file, finalPath, SYS_FILE_PATH_SIZE);
    return gSysInner->platStorage->WriteAll(finalPath, data, size);
}

int32_t SysFileAppend(const char* file, const char* fmt, ...)
{
    char finalPath[SYS_FILE_PATH_SIZE] = {0};
    char *line;
    va_list ap;
    int32_t ret;

    gSysInner->platStorage->GetObsolutePath(file, finalPath, SYS_FILE_PATH_SIZE);

    va_start(ap, fmt);
    line = new char[SYS_FILE_LINE_SIZE]();
    ret = vsnprintf(line, SYS_FILE_LINE_SIZE, fmt, ap);
    va_end(ap);

    if (ret > 0)
        ret = gSysInner->platStorage->Append(finalPath, line, ret);

    delete[] line;
    return ret;
}

